/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.controller.manager.adopt;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.common.annotation.Log;
import co.yixiang.common.annotation.NoRepeatSubmit;
import co.yixiang.common.constant.ShopConstants;
import co.yixiang.modules.adopt.entity.domain.YxAdoptProduct;
import co.yixiang.modules.adopt.entity.dto.YxAdoptProductDto;
import co.yixiang.modules.adopt.entity.dto.YxAdoptProductQueryCriteria;
import co.yixiang.modules.adopt.service.YxAdoptProductService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author hupeng
 * @date 2019-10-04
 */
@Api(tags = "认养:认养管理")
@RestController
@RequestMapping("api/yxAdoptProduct")
public class AdoptProductController {

    @Autowired
    private YxAdoptProductService yxAdoptProductService;


    @Log("查询认养")
    @ApiOperation(value = "查询认养")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','common','YXADOPTPRODUCT_ALL','YXADOPTPRODUCT_SELECT')")
    public ResponseEntity getYxAdoptProducts(YxAdoptProductQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(yxAdoptProductService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @NoRepeatSubmit
    @Log("新增认养")
    @ApiOperation(value = "新增认养")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @PostMapping
    @PreAuthorize("hasAnyRole('admin','common','common','YXADOPTPRODUCT_ALL','YXADOPTPRODUCT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YxAdoptProductDto resources) {
        if (ObjectUtil.isEmpty(resources.getGiveIntegral())) {
            resources.setGiveIntegral(BigDecimal.ZERO);
        }
        if (ObjectUtil.isEmpty(resources.getCost())) {
            resources.setCost(BigDecimal.ZERO);
        }
        return new ResponseEntity(yxAdoptProductService.saveProduct(resources), HttpStatus.CREATED);
    }

    @NoRepeatSubmit
    @Log("修改认养")
    @ApiOperation(value = "修改认养")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @PutMapping
    @PreAuthorize("hasAnyRole('admin','common','common','YXADOPTPRODUCT_ALL','YXADOPTPRODUCT_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YxAdoptProduct resources) {

        yxAdoptProductService.updateProduct(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @NoRepeatSubmit
    @Log("删除认养")
    @ApiOperation(value = "删除认养")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @DeleteMapping(value = "/{id}")
    @PreAuthorize("hasAnyRole('admin','common','common','YXADOPTPRODUCT_ALL','YXADOPTPRODUCT_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {

        yxAdoptProductService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "恢复数据")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @DeleteMapping(value = "/recovery/{id}")
    @PreAuthorize("hasAnyRole('admin','common','common','YXADOPTPRODUCT_ALL','YXADOPTPRODUCT_DELETE')")
    public ResponseEntity recovery(@PathVariable Integer id) {
        yxAdoptProductService.recovery(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "认养上架/下架")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @PostMapping(value = "/onsale/{id}")
    public ResponseEntity onSale(@PathVariable Integer id, @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        int status = Integer.valueOf(jsonObject.get("status").toString());
        yxAdoptProductService.onSale(id, status);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "生成属性")
    @PostMapping(value = "/isFormatAttr/{id}")
    public ResponseEntity isFormatAttr(@PathVariable Integer id, @RequestBody String jsonStr) {
        return new ResponseEntity(yxAdoptProductService.isFormatAttr(id, jsonStr), HttpStatus.OK);
    }

    @ApiOperation(value = "设置保存属性")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @PostMapping(value = "/setAttr/{id}")
    public ResponseEntity setAttr(@PathVariable Long id, @RequestBody String jsonStr) {
        yxAdoptProductService.createProductAttr(id, jsonStr);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "清除属性")
    @CacheEvict(cacheNames = ShopConstants.YSHOP_REDIS_INDEX_KEY, allEntries = true)
    @PostMapping(value = "/clearAttr/{id}")
    public ResponseEntity clearAttr(@PathVariable Long id) {
        yxAdoptProductService.clearProductAttr(id, true);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "获取属性")
    @GetMapping(value = "/attr/{id}")
    public ResponseEntity attr(@PathVariable Integer id) {
        String str = yxAdoptProductService.getAdoptProductAttrResult(id);
        if (StrUtil.isEmpty(str)) {
            return new ResponseEntity(HttpStatus.OK);
        }
        JSONObject jsonObject = JSON.parseObject(str);

        return new ResponseEntity(jsonObject, HttpStatus.OK);
    }


}
